package org.example.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.example.model.User;
import org.example.service.UserManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.camelgreen.baseclass.BaseRestSpringController;
import com.camelgreen.page.Page;
import com.camelgreen.page.PageParameter;

@Controller
@RequestMapping("/user")
public class UserControl extends BaseRestSpringController<User, String>{
	//	默认多列排序,example: name desc,id asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private final String LIST_ACTION = "redirect:/users";
	
	
	private UserManager userManager;
		
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	/** 列表 */
	@Override
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response,User user) {
			
		ModelAndView result = new ModelAndView();
		result.addObject("user",user);
		result.setViewName("/user/list");
		return result;
	}
	
	/** 进入新增 */
	@Override
	public ModelAndView _new(HttpServletRequest request,HttpServletResponse response,User user) throws Exception {
		return new ModelAndView("/user/new","user",user);
	}
	
	/** 显示 */
	@Override
	public ModelAndView show(@PathVariable java.lang.String id) throws Exception {
		User user = (User)userManager.getById(id);
		return new ModelAndView("/user/show","user",user);
	}
	
	/** 编辑 */
	@Override
	public ModelAndView edit(@PathVariable java.lang.String id) throws Exception {
		User user = (User)userManager.getById(id);
		return new ModelAndView("/user/edit","user",user);
	}
	
	/** 保存新增 */
	@Override
	public ModelAndView create(HttpServletRequest request,HttpServletResponse response,User user) throws Exception {
		userManager.save(user);
		return new ModelAndView(LIST_ACTION);
	}
	
	/** 保存更新 */
	@Override
	public ModelAndView update(@PathVariable java.lang.String id,HttpServletRequest request,HttpServletResponse response) throws Exception {
		User user = (User)userManager.getById(id);
		bind(request,user);
		userManager.update(user);
		return new ModelAndView(LIST_ACTION);
	}
	
	/** 删除 */
	@Override
	public ModelAndView delete(@PathVariable java.lang.String id) {
		userManager.removeById(id);
		return new ModelAndView(LIST_ACTION);
	}

	/** 批量删除 */
	@Override
	public ModelAndView batchDelete(java.lang.String[] items) {
		for(int i = 0; i < items.length; i++) {
			userManager.removeById(items[i]);
		}
		return new ModelAndView(LIST_ACTION);
	}
	
	
}
